<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-8-25
 * Time: 10:13
 */
namespace common\components\services\message;

use common\components\helpers\Fun;
use common\models\datas\LoginAuth;
use common\models\datas\Site;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\News;
use yii\base\Component;

class ManualWechatMessage extends Component
{
    public $url;

    public $options;

    public $title;

    public $desc;

    public $customer_id;

    public $data = [];


    public function __construct($data){
        $this->url = \Yii::$app->params['wechat_url'] . $data['url'];
        $this->title = $data['title'];
        $this->customer_id = $data['customer_id'];
        $this->desc = $data['desc'];
        $site = Site::findOne(Fun::getSiteId());
        $this->options = \Yii::$app->params['options'];
        if($site->mode == 2){
            $this->url = \Yii::$app->params['zhtwechat_url'] . $data['url'];
            $this->options = \Yii::$app->params['options_zht'];
        }
    }

    public function send()
    {
        $site = Site::findOne(Fun::getSiteId());
        $row = LoginAuth::find()->where(['user_id'=>$this->customer_id,'web_site_id'=>$site->web_site_id])->one();
        if($row != null) {
            $app = new Application($this->options);
            $news = new News([
                'title' => $this->title,
                'description' => $this->desc,
                'url' => $this->url
            ]);
            $res = $app->staff->message([$news])->to($row->source_id)->send();
            return $res->errcode;
        }
    }

}